# -*- coding: UTF-8 -*-
# Author: Damon(wuud1)
# CreateDate: 
# Message:

from base_class import BaseClass

class GetPartsPriceInfo(BaseClass):

    def __init__(self, **kwargs):
        self.db_config = kwargs.get("db_config")
        self.insert_table = kwargs.get("insert_table")
        self.conn, self.curs = self.get_conn_curs(db_config=self.db_config, type='dict')
        self.num = 0

    def get_pid_label(self):
        sql = '''select distinct `pid`,`label`,`brandCode`,`quantity` from `cars_part_num`'''
        self.curs.execute(sql)
        data = self.curs.fetchall()
        # pid_label = {i.get("pid"):{"label":i.get("label"),"brandCode":i.get("brandCode")} for i in data}
        return data

    def run(self):
        # 获取所有pid,label对照字典
        data = self.get_pid_label()
        _set = set()
        for info in data:
            pid = info.get("pid")
            label = info.get("label")
            brandCode = info.get("brandCode")
            quantity = info.get("quantity")
            _str = pid+brandCode
            if _str in _set:
                continue
            _set.add(_str)

            _dict = dict(
                pid=pid,
                label=label,
                channel='',
                name='',
                brandCode=brandCode,
                retailprice=0,
                qty=quantity,
                isreplace='0',
                iscomt='0',
                inventory='',
                condition='',
                createtime='now()',
                status='1',
            )
            insert_sql = self.construct_insert_sql(self.insert_table, _dict)
            try:
                self.curs.execute(insert_sql)
            except Exception as e:
                print(insert_sql)
                raise e
            self.num += 1
            if self.num % 1000 == 0:
                print("提交insert缓存, 当前数量: {}".format(self.num))
                self.conn.commit()
        self.conn.commit()
        print("最终提交, 总数据量: {}".format(self.num))


if __name__ == '__main__':
    db_config = dict(
        host="192.168.10.166",
        port=3307,
        user="a",
        passwd="110",
        db="jac",
        charset="utf8",
    )
    insert_table = 'parts_price_info'
    tools = GetPartsPriceInfo(db_config=db_config, insert_table=insert_table)
    tools.run()